package com.service.impl;

import com.dao.CourseMapper;
import com.domain.Course;
import com.domain.CourseVO;
import com.domain.Teacher;
import com.service.CourseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    /*
     * 多条件课程列表查询
     * */

    @Override
    public List<Course> findCourseByCondition(CourseVO courseVO) {
        return courseMapper.findCourseByCondition(courseVO);
    }

    /*
     * 添加课程和讲师信息
     * */
    @Override
    public void saveCourseOrTeacher(CourseVO courseVO){
        try {
            //补全信息
            Course course = new Course();
            BeanUtils.copyProperties(course,courseVO);

            Date date = new Date();
            course.setCreateTime(date);
            course.setUpdateTime(date);

            courseMapper.saveCourse(course);

            int courseId = course.getId();

            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher,courseVO);

            teacher.setCreateTime(date);
            teacher.setUpdateTime(date);
            teacher.setIsDel(0);

            teacher.setCourseId(courseId);

            courseMapper.saveTeacher(teacher);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    /*
     * 回显课程信息
     * */
    @Override
    public CourseVO findCourseById(Integer id) {
        return courseMapper.findCourseById(id);
    }
    /*
     * 更新课程和讲师信息
     * */
    @Override
    public void updateCourseOrTeacher(CourseVO courseVO) {
        try {
            Course course = new Course();
            BeanUtils.copyProperties(course,courseVO);

            Date date = new Date();
            course.setUpdateTime(date);

            courseMapper.updateCourse(course);

            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher,courseVO);

            teacher.setUpdateTime(date);
            teacher.setCourseId(courseVO.getId());

            courseMapper.updateTeacher(teacher);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    /*
     * 修改课程状态
     * */
    @Override
    public void updateCourseStatus(int id, int status) {
        Course course = new Course();

        course.setId(id);;
        course.setStatus(status);

        Date date = new Date();
        course.setUpdateTime(date);

        courseMapper.updateCourseStatus(course);
    }
}
